import frappe
from frappe.utils import connections

def patch_connections():
    """Apply patches for connections module"""
    if not hasattr(connections, '_original_is_open'):
        # Store the original function
        connections._original_is_open = connections.is_open
        
        def patched_is_open(scheme, hostname, port, timeout=10):
            if scheme == "dm":
                # Handle DM database connection check
                return connections._original_is_open("postgres", hostname, port, timeout)
            return connections._original_is_open(scheme, hostname, port, timeout)
            
        # Replace the original function with our patched version
        connections.is_open = patched_is_open
        print("connections.is_open patched successfully")

def revert_connections_patches():
    """Revert connection patches"""
    if hasattr(connections, '_original_is_open'):
        connections.is_open = connections._original_is_open
        del connections._original_is_open
